PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

Date

A complete Date value specifies the day of the week, the date (month, day of the month, and year), and the time; if you provide only some of this information, AppleScript fills in the missing pieces with default values. You can get and set properties of a Date value that correspond to different parts of the date and time information.

You can specify Date values in many different formats. The format always begins with the word date followed by a string (within quotation marks) containing the date and time information. You can spell out the day of the week, month, or date. You can also use standard three-letter abbreviations for the day and month.

When you compile a script, AppleScript displays date and time values according to the format specified in the Date & Time control panel.

For more information on arithmetic operations you can perform on dates and times, see Date-Time Arithmetic. For a description of how AppleScript handles century's end dates such as the year 2000, see Working With Dates at Century Boundaries.

LITERAL EXPRESSIONS

The following expressions show several options for specifying a date.

date "7/25/53, 12:06 PM" date "8/9/50, 12:06" date "8/9/50, 17:06" date "7/16/70" date "12:06" date "Sunday, December 12, 1954 12:06 pm"
PROPERTIES
Class
The class identifier for the object. This property is read-only, and its value is always date .
Day
An integer that specifies the day of the month of a date value.
Weekday
One of the constants Monday , Tuesday , Wednesday , Thursday , Friday , Saturday , Sunday or Mon , Tue , Wed , Thu , Fri , Sat , Sun .
Month
One of the constants January , February , March , April , May , June , July , August , September , October , November , December or Jan , Feb , Mar , Apr , May , Jun , Jul , Aug , Sep , Oct , Nov , Dec .
Year
An integer specifying the year; for example, 1993 .
Time
An integer that specifies the number of seconds since midnight of the date value; for example, 2700 is equivalent to 12:45 AM (2700 = 45 minutes times 60 seconds).
Date String
A string that consists of the date portion of the date value; for example, "Saturday, February 27, 1999" .
Time String
A string that consists of the time portion of the date value; for example, "3:20:24 PM" .
ELEMENTS

None

OPERATORS

The operators that take Date values as operands are &, +, -, =, ≠, >, ≥, <, ≤, Comes Before, Comes After, and As. In expressions containing >, ≥, <, ≤, Comes Before, or Comes After, a later time is greater than an earlier time. The following operations on Date values with the + and - operators are supported:

date + timeDifference
--result: date
date - date
--result: timeDifference
date - timeDifference
--result: date

where date is a Date value and timeDifference is an Integer value specifying a time difference in seconds. To simplify the notation of time differences, you can also use one or more of these of these constants:

minutes  60
hours
   60 * minutes
days
   24 * hours
weeks
   7 * days

Here's an example:

date "September 13, 1998" + 4 * days + 3 * hours + 2 * minutes
--result: date "Thursday, September 17, 1998 3:02:00 AM"

For more information about the way AppleScript operators treat Date values, see Date-Time Arithmetic.

REFERENCE FORMS

You can refer to properties of a Date value using the Property reference form. When you compile a script, AppleScript displays date and time values according to the format specified in the Date & Time control panel. The following statements access various date properties:

set theDate to current date --using scripting addition command
--result: date "Saturday, February 27, 1999 3:37:50 PM"
weekday of theDate --result: Saturday
day of theDate --result: 27
month of theDate --result: February
year of theDate --result: 1999
time of theDate --result: 56270 (seconds since 12:00:00 AM)
time string of theDate --result: "3:37:50 PM"
date string of theDate --result: "Saturday, February 27, 1999"

For more information on compiling dates, see the Notes section below.

If you want to specify a time relative to a date, you can do so by using of , relative to , or in , as shown in the following examples.

date "2:30 am" of date "March 3, 1999"
--result: date "Wednesday, March 3, 1999 2:30:00 AM"
date "Nov. 19, 1999" relative to date "3PM"
--result: date "Friday, November 19, 1999 3:00:00 PM"

date "1:30 pm" in date "March 3, 1999"
--result: date "Wednesday, March 3, 1999 1:30:00 PM"

If you set the Day property of a date to a value that does not fit into the month, the date rolls over to the next month:

set myDate to date "Thursday, February 4, 1999 12:00:00 AM"
set day of myDate to 37
myDate --result: date "Tuesday, March 9, 1999 12:00:00 AM"
COERCIONS SUPPORTED

AppleScript supports coercion of a Date value to a single-item list or a string.

NOTES

When you compile a script, AppleScript displays Date values in a format similar to the one shown in the following example, regardless of the format you use when you type the date. The compiled version includes the full name of the day of the week and month and no leading zeros for the date. The actual format is based on the settings in the Date & Time control panel. The following notes and examples assume the Date & Time control panel is set for 12-hour time, not 24-hour time.

date "Friday, January 3, 1992 12:05:00 PM"

If you don't specify a complete date, day, and time when typing a Date value, AppleScript fills in information as needed. If you don't specify the date information, AppleScript uses the date when the script is compiled. If you don't specify the time information, 12:00 AM (midnight) is the default. If you omit AM or PM, AM is the default; however, if you specify 12:00 without AM or PM, 12:00 PM is the default. If you specify a time using 24-hour time, AppleScript converts it to the equivalent time using AM or PM (when the Date & Time control panel is set for 12-hour time); for example, 17:00 is equivalent to 5:00 PM.

The following example shows how AppleScript fills in a default time property when the specified date doesn't include the time:

time string of date "March 3, 1999"
--result: "12:00:00 AM"

To get the current date, use the scripting addition command Current Date:

set theDate to current date
if (weekday of theDate) = Saturday then
    display dialog "I shouldn't be working today!"
end if

For more information about the Current Date and Display Dialog commands, and about the other standard scripting addition commands distributed with AppleScript, see the following website:

www.apple.com/applescript


© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)